<?php
session_start();
$con=mysqli_connect("localhost","root","root","gwc");
//登录
if($_POST["username"] && $_POST["password"] && $_POST["yzm"]==$_SESSION["yzm"]){
    //获取用户
    $userSql = mysqli_query($con,"select * from user where name='".$_POST["username"]."' and password='".$_POST["password"]."'");
    $user=mysqli_fetch_assoc($userSql);
    $_SESSION['user_id']=$user["id"];

}else if($_SESSION['user_id']=="" ||  $_POST["yzm"]!=$_SESSION["yzm"]) {
    header("Location:./login.html");
}
//商品操作
if(isset($_GET["pId"]) && isset($_GET["uId"]) && isset($_GET["num"])){
    $upSql= mysqli_query($con,"select * from user_product where product_id=".$_GET["pId"]." and user_id=".$_GET["uId"]);
    if (mysqli_num_rows($upSql) > 0) {
        //删除
        if ($_GET["num"] == 0) {
            mysqli_query($con, "delete from user_product where product_id=" . $_GET["pId"] . " and user_id=" . $_GET["uId"]);
        } else {
            //修改
            mysqli_query($con, "update user_product set num=" . $_GET["num"] . " where product_id=" . $_GET["pId"] . " and user_id=" . $_GET["uId"]);
        }
    } else {
        //添加
        mysqli_query($con, "insert into user_product (product_id,user_id,num) values(" . $_GET["pId"] . "," . $_GET["uId"] . "," . $_GET["num"] . ")");

    }
}
//获取商品
$productsSql = mysqli_query($con,"select product.*,user_product.num,user_product.user_id from product
                  LEFT JOIN user_product on user_product.product_id=product.id and user_product.user_id=".$_SESSION['user_id']." order by product.id");
$products=mysqli_fetch_all($productsSql,MYSQLI_ASSOC);
//删除商品
if(isset($_GET["del_id"])){
    if($_GET["del_id"]==0){
        mysqli_query($con,"delete from product");
    }else{
        mysqli_query($con,"delete from product where id=".$_GET["del_id"]);
    }
    header("Location: " . $_SERVER['PHP_SELF']);
}
mysqli_close($con);
//设置cookie
setcookie("products",serialize($products),time()+86400,"/");
$products=unserialize($_COOKIE["products"]);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>购物车</title>
    <link rel="stylesheet" href="css/style.css"/>
    <script type="text/javascript" src="js/jquery-3.7.1.min.js"></script>
    <script type="text/javascript" src="js/shoppingcart.js"></script>
</head>
<body>
<table id="cartTable">
    <thead>
    <tr>
        <th><label><input class="check-all check" type="checkbox" />&nbsp;全选</label></th>
        <th>商品</th>
        <th>单价</th>
        <th>数量</th>
        <th>小计</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tbody">
    <?php foreach($products as $key=>$product){ ?>
        <tr>
            <td class="checkbox"><input class="check-one check" type="checkbox"/></td>
            <td class="goods"><img src="<?=$product["picture"]; ?>" alt=""/ ><span class="product"><?=$product["name"]; ?></span></td>
            <td class="price"><?=$product["price"]; ?></td>
            <td class="count">
                <span class="reduce" onclick="cF(<?=$product["id"] ?>,<?=$_SESSION['user_id'] ?>,<?=$product["num"]==""? 1 : $product["num"]-1 ?>)"></span>
                <input class="count-input" type="text" value="<?=$product["num"]=="" ? 0 : $product["num"]; ?>"/>
                <span class="add" onclick="cF(<?=$product["id"] ?>,<?=$_SESSION['user_id'] ?>,<?=$product["num"]==""? 1 : $product["num"]+1 ?>)" >+</span></td>
            <td class="subtotal"><?=$product["price"]*$product["num"]; ?></td>
            <td class="operation"><span class="delete" onclick="delF(<?=$product["id"] ?>)">删除</span></td>
        </tr>
    <?php }; ?>

    </tbody>
</table>

<div class="foot" id="foot">
    <label class="fl select-all"><input type="checkbox" class="check-all check"/>&nbsp;全选</label>
    <a class="fl delete" id="deleteAll" href="javascript:;">删除</a>
    <div class="fr closing">结 算</div>
    <div class="fr total">合计：￥<span id="priceTotal">0.00</span></div>
    <div class="fr selected" id="selected">已选商品
        <span id="selectedTotal">0</span>件
        <span class="arrow up">︽</span>
        <span class="arrow down">︾</span>
    </div>
    <div class="selected-view">
        <div id="selectedViewList" class="clearfix">
            <!--<div><img src="images/1.jpg"><span>取消选择</span></div>-->
        </div>
        <span class="arrow">◆<span>◆</span></span>
    </div>
</div>
</body>
</html>